#define  _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void InitSeqList(SList* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
void DestroySeqList(SList* ps)
{
	assert(ps);
	if (ps->arr)
	{
		free(ps->arr);
		ps->arr = NULL;
		ps->capacity = 0;
		ps->size = 0;
	}
}
void CheckCapacity(SList* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity = 0 ? 8 : ps->capacity * 2;
		DataType* tmp = (DataType*)realloc(ps->arr, ps->capacity * sizeof(DataType));
		if (tmp == NULL)
		{
			printf("malloc is fail");
			return;
		}
		else
		{
			ps->capacity = newcapacity;
			ps->arr = tmp;
		}
	}
}
void PushBack(SList* ps,DataType x)
{
	assert(ps);
	CheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;
}
void PopBack(SList* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
void PushFront(SList* ps,DataType x)
{
	assert(ps);
	CheckCapacity(ps);
	int rear = ps->size-1;
	while (rear > -1)
	{
		ps->arr[rear + 1] = ps->arr[rear];
		rear--;
	}
	ps->arr[0] = x;
	ps->size++;
}
void PopFront(SList* ps)
{
	assert(ps);
	int begin = 1;
	while (begin < ps->size)
	{
		ps->arr[begin] = ps->arr[begin + 1];
		begin++;
	}
	ps->size--;
}
void Insert(SList* ps,int pos,DataType x)
{
	assert(ps);
	assert(pos > -1 && pos < ps->size + 1);
	CheckCapacity(ps);
	int cur = ps->size - 1;
	while (cur > pos - 1)
	{
		ps->arr[cur + 1] = ps->arr[cur];
		cur--;
	}
	ps->arr[pos] = x;
	ps->size++;
}
void Erase(SList* ps,int pos)
{
	assert(ps);
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->arr[begin - 1] = ps->arr[begin];
		begin++;
	}
	ps->size--;
}
int SListFind(SList* ps,DataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
			return i;
	}
	return -1;
}
void SListModify(SList* ps,DataType x,DataType y)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			ps->arr[i] = y;
		}
	}
}
void Print(SList* ps)
{
	assert(ps);
	int i = 0;
	for (i=0;i<ps->size;i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}
int main()
{
	SList s;
	InitSeqList(&s);
	PushBack(&s, 1);
	PushFront(&s, 2);
	Insert(&s, 2, 30);
	Erase(&s, 2);
	Print(&s);
	return 0;
}